<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="复核月度:">
                <Date-picker   v-model="queryParam.ccxx0001"  type="month" style="width: 186px"></Date-picker>
            </Form-item>
            <Form-item label="执行状态：">
                <Select v-model="queryParam.ccxx0002" placeholder="" style="width: 186px"   >
                    <Option value="0">待执行</Option>
                    <Option value="1">执行成功</Option>
                    <Option value="2">执行失败</Option>
                </Select>
            </Form-item>
            <Row class="button_row">
               <span style="float: left;">
                   <Button type="primary" @click="handleAdd()">制定计划</Button>
               </span>
                <span style="float: right;">
                   <Button type="primary" @click="searchQuery">查询</Button>
                <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
               </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="800" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules" inline>
                <FormItem label="复核月度:" prop="ccxx0001" :disabled="show_detail">
                    <Date-picker v-model="formData.ccxx0001"  type="month"  style="width: 186px"></Date-picker>
                </FormItem>
                <FormItem label="计划执行时间:" prop="ccxx0002" :disabled="show_detail">
                    <Date-picker   v-model="formData.ccxx0002"  type="date"  style="width: 186px"></Date-picker>
                </FormItem>
                <FormItem label="计划状态:" prop="ccxx0007" :disabled="show_detail">
                    <Select v-model="formData.ccxx0007" placeholder="" style="width: 186px"   >
                        <Option value="0">待执行</Option>
                        <Option value="1">执行成功</Option>
                        <Option value="2">执行失败</Option>
                    </Select>
                </FormItem>
                <FormItem label="执行结束时间:" prop="ccxx0010" :disabled="show_detail">
                    <Date-picker   v-model="formData.ccxx0010"  type="date"  style="width: 186px"></Date-picker>
                </FormItem>
                <FormItem label="复核数据总量:" prop="ccxx0003" :disabled="show_detail">
                    <Input v-model="formData.ccxx0003"   placeholder=""
                    />
                </FormItem>
                <FormItem label="匹配成功数量:" prop="ccxx0004" :disabled="show_detail">
                    <Input v-model="formData.ccxx0004"   placeholder=""
                           manual/>
                </FormItem>
                <FormItem label="匹配失败数量:" prop="ccxx0005" :disabled="show_detail">
                    <Input v-model="formData.ccxx0005"   placeholder=""
                           manual/>
                </FormItem>
                <FormItem label="下载失败数量:" prop="ccxx0006" :disabled="show_detail">
                    <Input v-model="formData.ccxx0006"   placeholder=""
                           manual/>
                </FormItem>
                <FormItem label="执行失败原因:" prop="ccxx0008" :disabled="show_detail">
                    <Input v-model="formData.ccxx0008"   placeholder="" type="textarea" :rows="2"
                           manual/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="visible = false">关闭</Button>
                <Button type="primary" @click="handleSubmit('formData')" v-if="showSubmit">确定</Button>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, index  }" slot="action">
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleEdit(index)">查看计划</Button>
                    <Button type="error" size="small" style="margin-right: 5px" @click="remove(row)">删除计划</Button>
<!--                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">查看详细</Button>-->
                    <Button type="success" size="small" style="margin-right: 5px" @click="monthFhHandle(row)">执行计划</Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"   ></Page>
        </div>
    </div>
</template>

<script>
    import { postAction } from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'
    import DateFormat from '@/plugins/format.js'
    export default {
        mixins: [CommonMixin],
        components: {
            //dictSelect
        },
        data() {
            return {
                rules: {
                    ccxx0001: [
                        { required: true, message: '复核月度不能为空', trigger: 'blur',
                            pattern: /.+/ }
                    ],
                    ccxx0002: [
                        { required: true, message: '计划执行时间不能为空', trigger: 'blur',
                            pattern: /.+/ }
                    ],
                },
                columns: [
                    {
                        title: '复核年月',
                        key: 'ccxx0001',
                        align: 'center',
                        width: '150'
                    },
                    {
                        title: '计划执行时间',
                        key: 'ccxx0002',
                        align: 'center',
                        width: '150',
                        render: (h, params) => {
                            let value = params.row.ccxx0002
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '执行状态',
                        key: 'ccxx0007',
                        align: 'center',
                        width: '130',
                        render: (h, params) => {
                            let value = params.row.ccxx0007
                            if ( value === '0' ) {
                                value = '待执行'
                            } else if ( value === '1' ) {
                                value  = '执行成功'
                            } else if ( value === '2' ) {
                                value  = '执行失败'
                            }
                            return h('span', value + ' ')
                        },
                    },
                    {
                        title: '复核数据总量',
                        key: 'ccxx0003',
                        align: 'center',
                        width: '150'
                    },
                    {
                        title: '保障数据匹配成功',
                        key: 'ccxx0004',
                        align: 'center',
                        width: '150'
                    },
                    {
                        title: '保障数据匹配失败',
                        key: 'ccxx0005',
                        align: 'center',
                        width: '150'
                    },
                    {
                        title: '属地区划匹配失败',
                        key: 'ccxx0011',
                        align: 'center',
                        width: '150'
                    },
                    {
                        title: '核对报告获取失败',
                        key: 'ccxx0006',
                        align: 'center',
                        width: '150'
                    }
                    ,{
                        title: '操作',
                        fixed: 'right',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: '300',
                        className: 'nopadding',
                    }
                ],
                tableData:[],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss08012Service/getPageList",
                    edit: "/api/ac/subsistence/ss08012Service/update",
                    add: "/api/ac/subsistence/ss08012Service/insert",
                    monthFhHandle: "/api/ac/subsistence/ss08012Service/updateMonthFhHandle"
                },
                formData: {},
                fieldsToValidate: ["ccxx0001","ccxx0002"],
                queryParam: {ccxx0012:'manual'},
                visible:false,
                show_detail:true,
                showSubmit:true,
                dataCount:0,
                pageSize:10,
                modal: false,
                title:""
            }
        },
        methods: {
            monthFhHandle (item) {
                this.$Modal.confirm({
                    title: '确定执行计划吗',
                    onOk: () => {
                        var dataParam = {
                            id: item.id,
                        }
                        postAction(this.url.monthFhHandle, dataParam).then((res) => {
                            this.$Notice.success({ title: '操作成功', desc: '' })
                            this.loadData();
                        })
                    }
                })
            },
            remove (item) {
                this.$Modal.confirm({
                    title: '确定该计划数据吗',
                    onOk: () => {
                        var dataParam = {
                            id: item.id,
                        }
                        postAction('/api/ac/subsistence/ss08012Service/delete', dataParam).then((res) => {
                            this.$Notice.success({ title: '操作成功', desc: '' })
                            this.loadData();
                        })
                    }
                })
            },
            handleAdd () {
                let that=this;
                that.title="行政区划录入"
                let record={};
                that.visible = true
                that.show_detail = false
                that.showSubmit = true
                that.formData = Object.assign({}, record)
                that.formData.ccxx0012='manual'
            },
            handleEdit (index) {
                let that=this;
                that.title="执行计划编辑"
                let record=that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.show_detail = false
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                    that.formData.ccxx0002 = new Date(record.ccxx0002)
                    if(record.ccxx0010!=undefined) {
                        that.formData.ccxx0010 = new Date(record.ccxx0010)
                    }
                } else {
                    that.visible = false
                    that.show_detail = true
                }
            },
            handleView (index) {
                let that=this;
                that.title="执行计划查看"
                let record=that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                    that.formData.ccxx0002 = new Date(record.ccxx0002)
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];
                for (let item of that.fieldsToValidate) {

                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(error);
                        }
                    })
                }
                if (validate) {
                    let that = this;
                    let dataParam = {
                        id: that.formData.id,
                        ccxx0001: DateFormat(that.formData.ccxx0001, 'YYYY-MM'),
                        ccxx0002: DateFormat(that.formData.ccxx0002, 'YYYY-MM-DD'),
                        ccxx0012: 'manual'
                    }
                    let url = null;
                    if (dataParam.id == null) {
                        url = that.url.add
                    } else {
                        url = that.url.edit
                    }
                    postAction(url, dataParam).then((res) => {
                        if (res.data.errcode == 0) {
                            that.visible = false
                            this.$Message.success("提交成功！")
                            this.loadData();
                        } else {
                            this.$Message.error(res.data.data.msg);
                        }
                    })
                }
            },
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    .paging {
        float: right;
        margin-top: 10px;
    }

    /deep/ .ivu-upload-list {
        display: none;
    }
</style>
